package pms.userkanri.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;

import pms.common.utils.Consts;
import pms.common.utils.PMSLog;
import pms.common.utils.StringValidator;
import pms.common.value.UserDataValue;
import pms.common.value.UserSessionValue;
import pms.userkanri.service.UserkanriService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Maniac
 *
 */
public class UserInsertAction extends ActionSupport {
	
	private static final long serialVersionUID = 2276244650030995925L;
	
	private UserkanriService userkanriService;

	private UserSessionValue userSessionValue;
	
	public UserSessionValue getUserSessionValue() {
		return userSessionValue;
	}

	public void setUserSessionValue(UserSessionValue userSessionValue) {
		this.userSessionValue = userSessionValue;
	}	
	
	private List userdatalist = new ArrayList();	
	
	public List getUserdatalist() {
		return userdatalist;
	}

	public void setUserdatalist(List userdatalist) {
		this.userdatalist = userdatalist;
	}

	private List userdatalist2 = new ArrayList();	
	
	public List getUserdatalist2() {
		return userdatalist2;
	}

	public void setUserdatalist2(List userdatalist2) {
		this.userdatalist2 = userdatalist2;
	}

	public UserkanriService getUserkanriService() {
		return userkanriService;
	}

	public void setUserkanriService(UserkanriService userkanriService) {
		this.userkanriService = userkanriService;
	}

	private UserDataValue userdatavalue=new UserDataValue();
	
	public UserDataValue getUserDataValue() {
		return userdatavalue;
	}

	public void setUserDataValue(UserDataValue userdatavalue) {
		this.userdatavalue = userdatavalue;
	}
	
	private String buttonId="";
	
	public String getButtonId() {
		return buttonId;
	}

	public void setButtonId(String buttonId) {
		this.buttonId = buttonId;
	}

	private String userCount = "";
	
	public String getUserCount() {
		return userCount;
	}

	public void setUserCount(String userCount) {
		this.userCount = userCount;
	}
	
	private String userId = "";
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	private String inuserid = "";	
	
	public String getInuserid() {
		return inuserid;
	}


	public void setInuserid(String inuserid) {
		this.inuserid = inuserid;
	}	
	
	private String inusername = "";	
	
	public String getInusername() {
		return inusername;
	}


	public void setInusername(String inusername) {
		this.inusername = inusername;
	}	
	
	private String inuserdispname = "";	
	
	public String getInuserdispname() {
		return inuserdispname;
	}


	public void setInuserdispname(String inuserdispname) {
		this.inuserdispname = inuserdispname;
	}

	private String inuserpassword = "";	
	
	public String getInuserpassword() {
		return inuserpassword;
	}


	public void setInuserpassword(String inuserpassword) {
		this.inuserpassword = inuserpassword;
	}	
	
	private String inuserpassword2 = "";	
	
	public String getInuserpassword2() {
		return inuserpassword2;
	}

	public void setInuserpassword2(String inuserpassword2) {
		this.inuserpassword2 = inuserpassword2;
	}		
	
	private String inuserkbn = "";	
	
	public String getInuserkbn() {
		return inuserkbn;
	}

	public void setInuserkbn(String inuserkbn) {
		this.inuserkbn = inuserkbn;
	}

	private String inkaisya = "";	
	
	public String getInkaisya() {
		return inkaisya;
	}

	public void setInkaisya(String inkaisya) {
		this.inkaisya = inkaisya;
	}
	
	private String ingroup = "";	
	
	public String getIngroup() {
		return ingroup;
	}

	public void setIngroup(String ingroup) {
		this.ingroup = ingroup;
	}	

	public UserDataValue getUserdatavalue() {
		return userdatavalue;
	}

	public void setUserdatavalue(UserDataValue userdatavalue) {
		this.userdatavalue = userdatavalue;
	}

	private String upuserid = "";	

	private String upusername = "";	
	
	private String upuserdispname = "";	
	
	private String upuserpassword = "";	
		
	private String upuserpassword2 = "";	
		
	private String upuserkbn = "";	
	
	private String upkaisya = "";	
	
	private String upgroup = "";	
	
	public String getUpuserid() {
		return upuserid;
	}

	public void setUpuserid(String upuserid) {
		this.upuserid = upuserid;
	}
	
	public String getUpusername() {
		return upusername;
	}

	public void setUpusername(String upusername) {
		this.upusername = upusername;
	}

	public String getUpuserdispname() {
		return upuserdispname;
	}

	public void setUpuserdispname(String upuserdispname) {
		this.upuserdispname = upuserdispname;
	}

	public String getUpuserpassword() {
		return upuserpassword;
	}

	public void setUpuserpassword(String upuserpassword) {
		this.upuserpassword = upuserpassword;
	}

	public String getUpuserpassword2() {
		return upuserpassword2;
	}

	public void setUpuserpassword2(String upuserpassword2) {
		this.upuserpassword2 = upuserpassword2;
	}

	public String getUpuserkbn() {
		return upuserkbn;
	}

	public void setUpuserkbn(String upuserkbn) {
		this.upuserkbn = upuserkbn;
	}

	public String getUpkaisya() {
		return upkaisya;
	}

	public void setUpkaisya(String upkaisya) {
		this.upkaisya = upkaisya;
	}

	public String getUpgroup() {
		return upgroup;
	}

	public void setUpgroup(String upgroup) {
		this.upgroup = upgroup;
	}
	
	public String UserInsert() throws ParseException {
		//チェック処理
		if(StringValidator.isEmpty(inusername)) {		
		   	addActionError(getText("validation.login.username"));
		   	PMSLog.debug("ユーザ名入力エラー発生");
			
		}	
		else if(StringValidator.isEmpty(inuserdispname)) {		
		   	addActionError(getText("validation.login.userdispname"));
		   	PMSLog.debug("ユーザ表示名入力エラー発生");
			
		}			
		else if(StringValidator.isEmpty(inuserpassword)) {		
		   	addActionError(getText("validation.login.password"));
		   	PMSLog.debug("ユーザパースワード入力エラー発生");
			
		}
		else if(StringValidator.isEmpty(inuserpassword2)) {		
		   	addActionError(getText("validation.login.password"));
		   	PMSLog.debug("ユーザパースワード入力エラー発生");
			
		}
		else if(!inuserpassword.equals(inuserpassword2)) {		
		   	addActionError(getText("validation.login.password2"));
		   	PMSLog.debug("ユーザパースワード入力エラー発生");
			
		}
		else if(StringValidator.isEmpty(inuserkbn)) {		
		   	addActionError(getText("validation.login.userkbn"));
		   	PMSLog.debug("ユーザ区分入力エラー発生");
			
		}		
		else if(StringValidator.isEmpty(inkaisya)) {		
		   	addActionError(getText("validation.login.kaisya"));
		   	PMSLog.debug("会社入力エラー発生");
			
		}
		else if(StringValidator.isEmpty(ingroup)) {		
		   	addActionError(getText("validation.login.group"));
		   	PMSLog.debug("グルプ入力エラー発生");
			
		}		
		if(this.hasActionErrors()) {
        	PMSLog.end();
    		return INPUT;
        }
		userdatavalue.setUserName(this.inusername);
		userdatavalue.setUserDispName(this.inuserdispname);	
		userdatavalue.setPassword(this.inuserpassword);
		userdatavalue.setUserRights(this.inuserkbn);
		userdatavalue.setUserGroupId(Integer.parseInt(this.ingroup));
		userdatavalue.setUserCompany(this.inkaisya);
		try {
			userkanriService.insert(this.userdatavalue);
			this.addActionMessage("ユーザが正しく登録されました");			
		} catch (ParseException ex) {
			this.addActionError("ユーザ登録エラー");
			throw ex;
		}		
		this.userdatavalue = new UserDataValue();	
		return SUCCESS;
	}		

	public String UserUpdate() throws ParseException {
		//チェック処理
		if(StringValidator.isEmpty(upusername)) {		
		   	addActionError(getText("validation.login.username"));
		   	PMSLog.debug("ユーザ名入力エラー発生");
			
		}	
		else if(StringValidator.isEmpty(upuserdispname)) {		
		   	addActionError(getText("validation.login.userdispname"));
		   	PMSLog.debug("ユーザ表示名入力エラー発生");
			
		}			
		else if(StringValidator.isEmpty(upuserpassword)) {		
		   	addActionError(getText("validation.login.password"));
		   	PMSLog.debug("ユーザパースワード入力エラー発生");
			
		}
		else if(StringValidator.isEmpty(upuserpassword2)) {		
		   	addActionError(getText("validation.login.password"));
		   	PMSLog.debug("ユーザパースワード入力エラー発生");
			
		}
		else if(!upuserpassword.equals(upuserpassword2)) {		
		   	addActionError(getText("validation.login.password2"));
		   	PMSLog.debug("ユーザパースワード入力エラー発生");
			
		}
		else if(StringValidator.isEmpty(upuserkbn)) {		
		   	addActionError(getText("validation.login.userkbn"));
		   	PMSLog.debug("ユーザ区分入力エラー発生");
			
		}		
		else if(StringValidator.isEmpty(upkaisya)) {		
		   	addActionError(getText("validation.login.kaisya"));
		   	PMSLog.debug("会社入力エラー発生");
			
		}
		else if(StringValidator.isEmpty(upgroup)) {		
		   	addActionError(getText("validation.login.group"));
		   	PMSLog.debug("グルプ入力エラー発生");
			
		}		
		if(this.hasActionErrors()) {
        	PMSLog.end();
    		return INPUT;
        }
		userdatavalue.setUserId(Integer.parseInt(this.userId));
		userdatavalue.setUserName(this.upusername);
		userdatavalue.setUserDispName(this.upuserdispname);	
		userdatavalue.setPassword(this.upuserpassword);
		userdatavalue.setUserRights(this.upuserkbn);
		userdatavalue.setUserGroupId(Integer.parseInt(this.upgroup));
		userdatavalue.setUserCompany(this.upkaisya);
	   	PMSLog.debug("user update");		
		try {
			userkanriService.update(this.userdatavalue);
			this.addActionMessage("ユーザが正しく更新されました");			
		} catch (ParseException ex) {
			this.addActionError("ユーザ更新エラー");
			throw ex;
		}		
		this.userdatavalue = new UserDataValue();	
		return SUCCESS;
	}
	
	public String doUpdate() {
		
		PMSLog.start();
	   	PMSLog.debug("doUpdate");
	   	PMSLog.debug("userId = " + userId);
	   	
		getUserData();		
		
		userdatalist2 = userkanriService.select(Integer.parseInt(userId),"2",0);		
		
		this.inusername = "";
		this.inuserdispname = "";
		this.inuserkbn = "";
		this.ingroup = "";
		this.inkaisya = "";
				
		UserDataValue l_userDataValue = (UserDataValue) userdatalist2.get(0);
	   	PMSLog.debug("UserName = " + l_userDataValue.getUserName());		
		
		this.upusername = l_userDataValue.getUserName();
		this.upuserdispname = l_userDataValue.getUserDispName();
		this.upuserpassword = l_userDataValue.getPassword();
		this.upuserpassword2 = l_userDataValue.getPassword();		
		this.upuserkbn = l_userDataValue.getUserRights();
		this.upgroup = Integer.toString(l_userDataValue.getUserGroupId());
		this.upkaisya = l_userDataValue.getUserCompany();	   	
		PMSLog.end();
		return INPUT;
	}	

	public String UserDelete() {
		
		PMSLog.start();
	   	PMSLog.debug("UserDelete");
	   	PMSLog.debug("userId = " + userId);
	   	
		getUserData();		
		
		if(StringValidator.isEmpty(userId)) {		
		   	addActionError(getText("validation.login.userID"));
		   	PMSLog.debug("ユーザを選択してください");
			
		}		
		if(this.hasActionErrors()) {
        	PMSLog.end();
    		return INPUT;
        }
		
		userdatavalue.setUserId(Integer.parseInt(this.userId));		
		userkanriService.delete(this.userdatavalue);
		this.userdatavalue = new UserDataValue();	
		   	
		PMSLog.end();
		return SUCCESS;
	}	
	
	public void  getUserData()  {
		int userid;
		String userRights = "";		
		int userGroupId ;
		
		HttpServletRequest l_request= ServletActionContext.getRequest();		
        HttpSession l_session = l_request.getSession(true);		
		userSessionValue = (UserSessionValue)l_session.getAttribute(Consts.USER_SESSION_KEY);
        
		userid = userSessionValue.getUserId();
		userRights = userSessionValue.getUserRights();			
		userGroupId = userSessionValue.getUserGroupId();
		   	
		userdatalist = userkanriService.select(userid,userRights,userGroupId);				
		userCount = Integer.toString(userdatalist.size());
	}
	
	@Override
	public String execute() throws ParseException {
		PMSLog.start();
		
		getUserData();		
		
		if(buttonId.equals("登　　録")) {
			return UserInsert();				
		} else if(buttonId.equals("更　　新")) {
			return UserUpdate();
		} else if(buttonId.equals("削　　除")) {
			return UserDelete();
		} else {
    		return INPUT;		
		}
	}
}
